export default {
	state: {
		list: [{
				checked: false, //是否选中
				id: 11,
				title: "商品标题111",
				cover: "/static/images/demo/list/1.jpg",
				pprice: 200,
				num: 1, //当前的值
				minnum: 1,
				maxnum: 10, // 该商品最大商品数，跟库存有关
			},
			{
				checked: false, //是否选中
				id: 12,
				title: "商品标题222",
				cover: "/static/images/demo/list/2.jpg",
				pprice: 100,
				num: 2,
				minnum: 1,
				maxnum: 10, // 该商品最大商品数，跟库存有关
			},
			{
				checked: false, //是否选中
				id: 13,
				title: "商品标题333",
				cover: "/static/images/demo/list/3.jpg",
				pprice: 300,
				num: 1,
				minnum: 1,
				maxnum: 10, // 该商品最大商品数，跟库存有关
			}
		],
		selectedAll: [], //放选中的id
	},
	getters: {
		checkedAll(state) {
			return state.list.length == state.selectedAll.length
		},
		//总价
		totalPrice(state) {
			let total = 0;
			state.list.forEach(item => {
				if (item.checked) {
					total += item.pprice * item.num
				}
				return total;
			}, 0)
			return total;
		},
	},
	mutations: {
		//单选
		selectItem(state, index) {
			let id = state.list[index].id
			let i = state.selectedAll.indexOf(id)
			//当前找到选中的
			if (i > -1) {
				//选中的变成未选中的
				state.list[index].checked = false
				//state.selectedAll里面的id删掉
				state.selectedAll.splice(i, 1)
			} else { //没有选中的
				state.list[index].checked = true
				state.selectedAll.push(id)
			}
			// console.log(state.selectedAll);
		},
		//全选
		selectAll(state) {
			state.selectedAll = state.list.map(item => {
				item.checked = true
				return item.id
			})
			// console.log(state.selectedAll);
		},

		//全不选
		unSelectAll(state) {
			state.list.forEach(item => {
				item.checked = false
				state.selectedAll = []
			})
		},
		//删除
		delGoods(state) {
			uni.showModal({
				title: '提示',
				content: '你确定要删除吗？',
				success: function(res) {
					if (res.confirm) {
						state.list = state.list.filter(item => {
							return !item.checked
						})
						state.selectedAll = []
					} else if (res.cancel) {
						return
					}
				}
			})
		}
	},
	actions: {
		doSelectAll({
			//context解构
			getters,
			commit
		}) {
			// console.log('context', context);
			getters.checkedAll ? commit('unSelectAll') : commit('selectAll')
		},
	}

}